﻿using System;
using System.Collections.Generic;
using System.Text;

namespace NDK.Module.Model.ACMTMUR
{
    [Serializable]
    public class MURMesConfig
    {
        public string URL { get; set; }

        /// <summary>
        /// 设备编号
        /// </summary>
        public string MachineId { get; set; }

        /// <summary>
        /// 工站编号
        /// </summary>
        public string StationCode { get; set; }

        /// <summary>
        /// 机种编号
        /// </summary>
        public string MdlCode { get; set; }

        /// <summary>
        /// 品质组
        /// </summary>
        public string QCode_Product1 { get; set; }
        public string QCode_Product2 { get; set; }
        public string QCode_Block { get; set; }

        public string RunStatusStr { get; set; }

        public string RunningTroubleStr { get; set; }

        public string TroubleRecoveryStr { get; set; }

        public string RunningAlarmStr { get; set; }

        public string DefectInformationStr { get; set; }

        public string QCInformationStr { get; set; }
    }

    public class MURMesInfo
    {
        public MURMesInfo(MURMesConfig config, MesType type)
        {
            Config = config;
            Type = type;
        }

        protected MURMesConfig Config { get; set; }
        public MesType Type { get; set; }

        /// <summary>
        /// 时间格式: yyyyMMddHHmmss
        /// </summary>
        public string Timestamp { get; set; }

        /// <summary>
        /// 机种编号
        /// </summary>
        public string MdlCode { get; set; }

        /// <summary>
        /// 设备稼动状态 
        /// </summary>
        public string RunStatus { get; set; } = "";

        public string RunMode { get; set; } = "";

        public string LocalUnitld { get; } = "ACMT";

        public string TroubleAlarmCode { get; set; } = "";

        public string LotNo { get; set; } = "";

        public string DefectCode { get; set; } = "";

        public string Remarks { get; set; } = "";

        public MURRowData RowDatas { get; set; }

        public override string ToString()
        {
            string Info = null;
            switch (Type)
            {
                case MesType.RunStatus:
                    Info = Config.RunStatusStr;
                    break;
                case MesType.TroubleRecovery:
                    Info = Config.TroubleRecoveryStr;
                    break;
                case MesType.RunningTrouble:
                    Info = Config.RunningTroubleStr;
                    break;
                case MesType.RunningAlarm:
                    Info = Config.RunningAlarmStr;
                    break;
                case MesType.DefectInformation:
                    Info = Config.DefectInformationStr;
                    break;
                case MesType.QCInformation:
                    Info = Config.QCInformationStr;
                    break;
                default:
                    break;
            }
            foreach (var item in GetType().GetProperties())
            {
                if (Info.Contains($"{item.Name}A"))
                {
                    Info = Info.Replace($"{item.Name}A", item.GetValue(this).ToString());
                }
            }
            foreach (var item in Config.GetType().GetProperties())
            {
                if (Info.Contains($"{item.Name}A"))
                {
                    Info = Info.Replace($"{item.Name}A", item.GetValue(Config).ToString());
                }
            }
            return Info;
        }
    }

    public class MURRowData
    {
        public string Time { get; set; }

        public string SN { get; set; }

        public string MdlCode { get; set; }

        public string Code { get; } = "01";

        public string Pos { get; set; }

        public string QCCode_Product1 { get; set; }
        public string QCCode_Product2 { get; set; }
        public string QCCode_Block { get; set; }

        public string Judge { get; set; }

        public List<MURMeaure> Measures { get; set; }

        public override string ToString()
        {
            if (Common.whichBlock == -1)
            {
                int no = 0;
                StringBuilder str1 = new StringBuilder();
                StringBuilder str2 = new StringBuilder();
                for (int i = 0; i < Measures.Count; i++)
                {
                    str1.Append($"{Measures[i].Judge},{Measures[i].Value},");
                    if (Measures[i].Name == "FAI44")
                    {
                        no = i; break;
                    }
                }
                for (int i = no + 1; i < Measures.Count; i++)
                {
                    str2.Append($"{Measures[i].Judge},{Measures[i].Value},");
                }
                return $"{Time},{SN},{MdlCode},{Code},{QCCode_Product1},{Judge},0,{Pos},0,{Judge},{str1}\",\"{Time},{SN},{MdlCode},{Code},{QCCode_Product2},{Judge},{str2}";
            }
            else
            {
                StringBuilder str1 = new StringBuilder();
                foreach (var item in Measures)
                {
                    str1.Append($"{item.Judge},{item.Value},");
                }
                return $"{Time},{SN},{MdlCode},{Code},{QCCode_Block},{Judge},{str1}\",\"";
            }
        }
    }

    public class MURMeaure
    {
        public string Name { get; set; }
        public string Judge { get; set; }

        public string Value { get; set; }
    }

    public enum MesType
    {
        RunStatus,
        RunningTrouble,
        TroubleRecovery,
        RunningAlarm,
        DefectInformation,
        QCInformation
    }
}
